﻿using System;
using System.Net;
using System.Net.Configuration;
using System.Net.Mail;
using System.Web;
using System.Web.Configuration;

namespace VASJ.Model
{
    public class MailHelper
    {
        public static bool SendEmail(string toAddress, string ccAddress, string bccAddress, string subject, string body, MailPriority priority, bool isHtml)
        {
            try
            {
                var smtpClient = new SmtpClient();
                using (var message = new MailMessage())
                {
                    var configurationFile = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
                    var mailSettings = configurationFile.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;
                    if (mailSettings != null)
                    {
                        var fromAddress = new MailAddress(mailSettings.Smtp.From);

                        smtpClient.Host = mailSettings.Smtp.Network.Host;
                        smtpClient.Port = mailSettings.Smtp.Network.Port;

                        bool bAuthenticate = WebConfigurationManager.AppSettings["Authenticate"].ToLower() == "true";
                        if (bAuthenticate == true)
                        {
                            smtpClient.UseDefaultCredentials = false;
                            smtpClient.EnableSsl = true;
                            smtpClient.Credentials = new NetworkCredential(mailSettings.Smtp.Network.UserName, mailSettings.Smtp.Network.Password);
                        }
                        else
                        {
                            smtpClient.UseDefaultCredentials = true;
                            smtpClient.EnableSsl = false;
                        }

                        smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;

                        message.From = fromAddress;
                        message.Priority = priority;

                        message.To.Add(toAddress);
                        message.Subject = subject;

                        if (ccAddress.Length > 0)
                        {
                            message.CC.Add(ccAddress);
                        }
                        if (bccAddress.Length > 0)
                        {
                            message.Bcc.Add(bccAddress);
                        }

                        //Body can be Html or text format
                        //Specify true if it is html message
                        message.IsBodyHtml = isHtml;

                        // Message body content
                        message.Body = body;

                        // Send SMTP mail
                        smtpClient.Send(message);
                    }
                }

                return true;
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}